//
//  YTStudyOneViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/11/13.
//

#import "YTStudyOneViewController.h"
#import "YTStashTJModel.h"
#import "YTStudyOneTableViewCell.h"
#import "YTStudySearchViewController.h"
#import "YTStudyDetailsViewController.h"
#import "YTVideoViewController.h"

@interface YTStudyOneViewController ()

@property (nonatomic ,strong) UIView * btnView;
@property (nonatomic ,strong) NSMutableArray <YTStashTJModel *>* modelArray;
@property (nonatomic ,strong) NSMutableArray <YTStashOneModel *>* BTmodelArray;
@property (nonatomic ,strong) gywbtn * seledBtn;
@property (nonatomic ,strong) NSURLSessionDataTask * task;

@end

@implementation YTStudyOneViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if(self.BTmodelArray.count > 0){
        [self refreshingData];
    }
}

- (NSMutableArray<YTStashTJModel *> *)modelArray{
    if(!_modelArray){
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}
- (NSMutableArray<YTStashOneModel *> *)BTmodelArray{
    if(!_BTmodelArray){
        _BTmodelArray = [NSMutableArray array];
    }
    return _BTmodelArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView * topview = [UIView new];
    [self.view addSubview:topview];
    topview.backgroundColor = KcolorHex(@"#0967FF");
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.left.offset(0);
        make.height.offset(KTop + kwt(56));
    }];
    MJWeakSelf
    UIView * backview = [UIView new].addClickBlock(^{
        [weakSelf popViewController];
    });
    [topview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(kwt(56));
        make.left.bottom.offset(0);
        make.width.offset(kwt(52));
    }];
    
    UIImageView * leftimg = kimageView(@"white_path");
    [backview addSubview:leftimg];
    [leftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(20));
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    UIView * ssview = [UIView new];
    ssview.addClickBlock(^{
        [weakSelf openSS];
    });
    ssview.backgroundColor = UIColor.whiteColor;
    [topview addSubview:ssview];
    [ssview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(52));
        make.height.offset(kwt(40));
        make.right.offset(kwt(-18));
        make.centerY.equalTo(leftimg);
    }];
    [DTKTool YJ:kwt(5) andView:ssview];
    
    UIImageView * ssimg = kimageView(@"program_search");
    [ssview addSubview:ssimg];
    [ssimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.width.height.offset(kwt(16));
        make.centerY.offset(0);
    }];
    
    UILabel * sslab = [DTKTool labelWithText:@"请搜索" andFont:kFont(16) andTextColor:KcolorHex(@"#89908D")];
    [ssview addSubview:sslab];
    [sslab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ssimg.mas_right).offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    UIView * btnView = [UIView new];
    self.btnView = btnView;
    [self.view addSubview:btnView];
    [btnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(topview.mas_bottom);
        make.height.offset(0);
    }];
    [self BtnApi];
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.equalTo(btnView.mas_bottom);
    }];
    [self addHeaderView:^{
        [weakSelf refreshingData];
    }];
    [self addFooterView:^{
        [weakSelf addMoreData];
    }];
}
- (void)openSS{
    YTStudySearchViewController * vc= [YTStudySearchViewController new];
    if(self.seledBtn){
        vc.parentId = [self.seledBtn.infoDict objectForKey:@"parentId"];
    }
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(void)refreshingData{
    self.pageNo = 1;
    [self.modelArray removeAllObjects];
    [self loadNewData];
}

-(void)addMoreData{
    self.pageNo += 1;
    [self loadNewData];
}

- (void)BtnApi{
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setObject:@(2) forKey:@"level"];
    [dict setObject:self.parentId forKey:@"parentId"];

    [SCNetManger learnTypeTypeOptionWithDict:dict andCompetionHandlerBlock:^(HTTPArrBase * model) {
        if(model.code == 200){
            NSArray * array = [NSArray yy_modelArrayWithClass:YTStashOneModel.class json:model.result];
            YTStashOneModel * m = [YTStashOneModel new];
            m.ID = self.parentId;
            m.typeName = @"全部";
            [self.BTmodelArray addObject:m];
            [self.BTmodelArray addObjectsFromArray:array];
            [self addbtn];
        } else {
            [self.view showWarning:model.message];
        }
    }];
}
- (void)addbtn{
    if(self.BTmodelArray.count > 1){
        [self.btnView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.offset(kwt(59));
        }];
        
        UIScrollView * scrollview = [[UIScrollView alloc] init];
        scrollview.showsVerticalScrollIndicator = NO;
        scrollview.showsHorizontalScrollIndicator = NO;

        [self.btnView addSubview:scrollview];
        [scrollview mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.right.left.bottom.offset(0);
        }];
        
        
        CGFloat X = kwt(10);
        
        
        for (YTStashOneModel * model in self.BTmodelArray) {
            gywbtn * btn = [gywbtn new];
            btn.infoDict = @{@"parentId":model.ID};
            [btn setTitle:model.typeName forState:UIControlStateNormal];
            [btn setTitleColor:KcolorHex(@"#717171") forState:UIControlStateNormal];
            btn.backgroundColor = UIColor.whiteColor;
            [btn setTitleColor:KcolorHex(@"#0967FF") forState:UIControlStateSelected];
            btn.titleLabel.font = kFont(16);
            [scrollview addSubview:btn];
            btn.frame = CGRectMake(X, kwt(10), model.typeNameW, kwt(37));
            btn.addTarget(self,@selector(btnClick:));
            X += model.typeNameW + kwt(10);
            if(!self.seledBtn){
                [self btnClick:btn];
            }
        }
        scrollview.contentSize = CGSizeMake(X, 0);
        
    } else {
        [self refreshingData];
        [self.btnView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.offset(0);
        }];
    }
}
- (void)btnClick:(gywbtn *)btn{
    if(self.seledBtn == btn){
        return;
    }
    self.seledBtn.selected = NO;
    [DTKTool YJBK:kwt(5) andView:self.seledBtn andWidth:0.5 andColor:KcolorHex(@"#EEEDED")];
    self.seledBtn = btn;
    self.seledBtn.selected = YES;
    [DTKTool YJBK:kwt(5) andView:self.seledBtn andWidth:0.5 andColor:KcolorHex(@"#0967FF")];
    [self refreshingData];
}
- (void)loadNewData{
    if(self.task){
        [self.task cancel];
        [self.view hideBusyHUD];
    }
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    if(self.BTmodelArray.count > 1){
        if(self.seledBtn){
            [dict setObject:[self.seledBtn.infoDict objectForKey:@"parentId"] forKey:@"typeId"];
        }
    } else {
        [dict setObject:self.parentId forKey:@"typeId"];

    }
    //    [dict setObject:@(2) forKey:@"level"];
    [dict setObject:@(self.pageNo) forKey:@"pageNo"];
    [dict setObject:@(self.pageSize) forKey:@"pageSize"];
    self.task = [SCNetManger learnInfoGetLearnInfoListWithDict:dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        self.task = nil;
        if(model.code == 200){
            NSArray * array = [NSArray yy_modelArrayWithClass:YTStashTJModel.class json:model.result[@"records"]];
            if(array.count == 0 && self.modelArray.count > 0){
                [self.tableview.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.tableview.mj_footer endRefreshing];
            }
            [self.tableview.mj_header endRefreshing];
            [self.modelArray addObjectsFromArray:array];
            
            [self.tableview reloadData];
        } else {
            [self.view showWarning:model.message];
        }
    }];

}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return kwt(113);
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    YTStudyOneTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"YTStudyOneTableViewCell"];
    if (cell == nil) {
        cell = [[YTStudyOneTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YTStudyOneTableViewCell"];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.modelArray[indexPath.row];
    MJWeakSelf
    cell.ReloadDataBlock = ^{
        [weakSelf.tableview reloadData];
    };
    cell.openvVideoBlock = ^(NSString * _Nonnull url) {
        YTVideoViewController * vc = [YTVideoViewController new];
        vc.url = url;
        vc.titleStr = weakSelf.modelArray[indexPath.row].title;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    };
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if([self.modelArray[indexPath.row].displayForm isEqualToString:@"1"]){
//        [DTKTool openImagAndVideo:@[self.TJModelArray[indexPath.row].learnFileUrl] andopenview:self.view andIndex:0];
        YTVideoViewController * vc = [YTVideoViewController new];
        vc.url = self.modelArray[indexPath.row].learnFileUrl;
        vc.titleStr = self.modelArray[indexPath.row].title;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    } else{
        YTStudyDetailsViewController * vc = [YTStudyDetailsViewController new];
        vc.ID = self.modelArray[indexPath.row].ID;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

@end
